package com.yl.nettylearning.nio;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author yangl
 */
public class FileChannelDemo {
    public static void main(String[] args) throws Exception{
        String str = "hello";
        //创建一个输出流--获取一个channel
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\test01.txt");
        //通过FileOutputStream获取对应 的FileChannel
        //这个FileChannel真实类型是FileChannelImpl
        FileChannel channel = fileOutputStream.getChannel();
        //创建一个缓冲区ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //将str放入到byteBuffer
        byteBuffer.put(str.getBytes());
        //对buffer反转
        byteBuffer.flip();
        //将byteBuffer数据写入到FileChannel
        channel.write(byteBuffer);
        fileOutputStream.close();
    }
}
